package com.example.jack.community.view.login;

import android.content.Intent;
import android.os.Bundle;
import android.support.percent.PercentRelativeLayout;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.chaychan.viewlib.PowerfulEditText;
import com.example.jack.community.BaseActivity;
import com.example.jack.community.R;
import com.example.jack.community.data.loginbean.Code;
import com.example.jack.community.view.login.util.CountDownTimerUtils;
import com.google.gson.Gson;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;


/**
 * 忘记密码验证码页
 */
public class Forget_aActivity extends BaseActivity {

    @BindView(R.id.forgetEdiPhone)
    PowerfulEditText forgetEdiPhone;
    @BindView(R.id.forgetEdiCode)
    EditText forgetEdiCode;
    @BindView(R.id.login_button)
    Button loginButton;
    @BindView(R.id.activity_forget_a)
    PercentRelativeLayout activityForgetA;
    @BindView(R.id.forgetTextCode)
    TextView forgetTextCode;
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.txt_back)
    TextView txtBack;
    private EventHandler eventHandler;
    private static final String TAG = "logins";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_activity_forget_a);
        ButterKnife.bind(this);
        //判断验证码是否正确的回调
        code();
        // 注册监听器
        SMSSDK.registerEventHandler(eventHandler);
    }

    @OnClick({R.id.forgetTextCode, R.id.login_button, R.id.back,R.id.txt_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.txt_back:
                finish();
                break;
            case R.id.back:
                finish();
                break;
            case R.id.forgetTextCode:
                Log.d(TAG, "onViewClicked: " + forgetEdiPhone.getText().toString().trim());
                CountDownTimerUtils mCountDownTimerUtils = new CountDownTimerUtils(forgetTextCode, 60000, 1000);
                mCountDownTimerUtils.start();
                SMSSDK.getVerificationCode("86", forgetEdiPhone.getText().toString().trim());
                break;
            case R.id.login_button:
                String code = forgetEdiCode.getText().toString().trim();
                String phonenum = forgetEdiPhone.getText().toString().trim();
                SMSSDK.submitVerificationCode("86", phonenum, code);
                break;
        }
    }

    /**
     * 验证码回调函数
     */
    private void code() {
        // 创建EventHandler对象
        eventHandler = new EventHandler() {
            public void afterEvent(int event, int result, Object data) {
                if (data instanceof Throwable) {
                    Throwable throwable = (Throwable) data;
                    final String msg = throwable.getMessage();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Gson gson = new Gson();

                            Code shopInfo = gson.fromJson(msg, Code.class);

                            Log.d(TAG, "run: " + shopInfo.getStatus() + msg);
                            if (shopInfo.getStatus() == 468) {
                                Toast.makeText(Forget_aActivity.this, "输入的验证码错误,请重新输入", Toast.LENGTH_SHORT).show();
                            }
                            if (shopInfo.getStatus() == 603) {
                                Toast.makeText(Forget_aActivity.this, "请填写正确的手机号码", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });

                } else {
                    if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {

                        // 处理你自己的逻辑
                        Log.d(TAG, "afterEvent: 获取成功");
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(Forget_aActivity.this, "验证码获取成功,请注意查收", Toast.LENGTH_SHORT).show();
                            }
                        });
                    } else if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
                        //提价验证码
                        Log.d(TAG, "afterEvent: 验证成功");
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                String phonenum = forgetEdiPhone.getText().toString().trim();
                                Intent intent = new Intent(Forget_aActivity.this, Forget_bActivity.class);
                                intent.putExtra("phonenum", phonenum);
                                startActivity(intent);
                            }
                        });
                    }
                }
            }
        };
    }
}
